package com.dr.sonar.report.sonar;

import com.alibaba.fastjson2.JSON;
import com.dr.sonar.report.config.SonarConfig;
import com.dr.sonar.report.common.HttpClientUtil;
import com.dr.sonar.report.sonar.bean.ProjectPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Package: com.netease.sonarreport.sonar
 * @Description: sonarqube api client
 * @Author: rayduan
 * @CreateDate: 2023/5/6 14:58
 * @Version: 1.0
 * <p>
 */
@Component
public class SonarApiClient {

    @Autowired
    private SonarConfig sonarConfig;


    /**
     * 获取项目列表
     *
     * @return {@link ProjectPageInfo}
     */
    public ProjectPageInfo getProjects() {
        String url = sonarConfig.getSonarUrl() + "api/projects/search";
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "Basic " + sonarConfig.getSonarToken());
        Map<String, String> params = new HashMap<>();
        params.put("p", "1");
        params.put("ps", "20");
        String response = HttpClientUtil.doGet(url, params, headers);
        return JSON.parseObject(response, ProjectPageInfo.class);
    }



}
